package com.carp.utils;

import jdk.nashorn.internal.objects.NativeString;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;

public class MD5Utils {

    public static String md5(String src){
        return DigestUtils.md2Hex(src);
    }

    //盐值
    private static String salt = "1a2b3c4d";

    //第一次加密
    public static String inputPassToFromPass(String inputPass){
        //这里的salt.charAt获取字符,自己随便取
        String str = salt.charAt(0)+salt.charAt(1)+inputPass+salt.charAt(5)+salt.charAt(5);
        return md5(str);
    }

    //第二次加密
    public static String fromPassToDBPass(String formPass,String salt){
        String str = salt.charAt(0)+salt.charAt(1)+formPass+salt.charAt(5)+salt.charAt(5);
        return md5(str);
    }

    //调用方法-获取加密后的密码
    public static String inputPassToDBPass(String inputPass,String salt){
        String fromPass = inputPassToFromPass(inputPass);
        String dbPass = fromPassToDBPass(fromPass, salt);

        return dbPass;
    }

    public static void main(String[] args) {
        //第一次加密的密码
        System.out.println(inputPassToFromPass("123456"));
        //第二次加密的密码
        System.out.println(fromPassToDBPass("a497d3a0e321c587942006abc6abbdd0","1a2b3c4d"));
        //最终存入数据库加密后的密码,如果第二次打印结果和本次打印一样,就表示加密成功
        System.out.println(inputPassToDBPass("123456","1a2b3c4d"));
    }
}
